from google.appengine.ext import db

from gaeo.controller import BaseController

from model.link import Link

class LinkController(BaseController):
    def create(self):
        r = Link(
            desc = self.params.get('desc', None),
            private = self.params.get('private', None),
            title = self.params.get('title', None),
            url = self.params.get('url', None),
            comment = self.params.get('comment', None),
            percentage = self.params.get('percentage', None),
            stage = self.params.get('stage', None),
            deadline = self.params.get('deadline', None),
            priority = self.params.get('priority', None),
        )
        r.put()
        self.redirect('/link')

    def destroy(self):
        r = Link.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/link')

    def edit(self):
        rec = Link.get(self.params.get('key'))
        if rec:
            setattr(self, 'desc', rec.desc)
            setattr(self, 'private', rec.private)
            setattr(self, 'title', rec.title)
            setattr(self, 'url', rec.url)
            setattr(self, 'comment', rec.comment)
            setattr(self, 'percentage', rec.percentage)
            setattr(self, 'stage', rec.stage)
            setattr(self, 'deadline', rec.deadline)
            setattr(self, 'priority', rec.priority)
        else:
            setattr(self, 'desc', 'Error')
            setattr(self, 'private', 'Error')
            setattr(self, 'title', 'Error')
            setattr(self, 'url', 'Error')
            setattr(self, 'comment', 'Error')
            setattr(self, 'percentage', 'Error')
            setattr(self, 'stage', 'Error')
            setattr(self, 'deadline', 'Error')
            setattr(self, 'priority', 'Error')

    def index(self):
        query = Link.all()
        self.result = query

    def new(self):
        pass

    def show(self):
        rec = Link.get(self.params.get('key'))
        if rec:
            setattr(self, 'desc', rec.desc)
            setattr(self, 'private', rec.private)
            setattr(self, 'title', rec.title)
            setattr(self, 'url', rec.url)
            setattr(self, 'comment', rec.comment)
            setattr(self, 'percentage', rec.percentage)
            setattr(self, 'stage', rec.stage)
            setattr(self, 'deadline', rec.deadline)
            setattr(self, 'priority', rec.priority)
        else:
            setattr(self, 'desc', 'Error')
            setattr(self, 'private', 'Error')
            setattr(self, 'title', 'Error')
            setattr(self, 'url', 'Error')
            setattr(self, 'comment', 'Error')
            setattr(self, 'percentage', 'Error')
            setattr(self, 'stage', 'Error')
            setattr(self, 'deadline', 'Error')
            setattr(self, 'priority', 'Error')

    def update(self):
        r = Link.get(self.params.get('key'))
        r.desc = self.params.get('desc', None)
        r.private = self.params.get('private', None)
        r.title = self.params.get('title', None)
        r.url = self.params.get('url', None)
        r.comment = self.params.get('comment', None)
        r.percentage = self.params.get('percentage', None)
        r.stage = self.params.get('stage', None)
        r.deadline = self.params.get('deadline', None)
        r.priority = self.params.get('priority', None)
        r.put()
        self.redirect('/link')
